package com.booksystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booksystem.dto.ProductDTO;
import com.booksystem.entity.ProductInfo;
import com.booksystem.mapper.ProductMapper;
import com.booksystem.service.ProductService;
import java.util.Date;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品信息服务实现类
 * </p>
 * @author 段伟豪
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductInfo> implements ProductService {

    @Override
    public Boolean addProduct(ProductDTO productDTO) {
        // 校验商品名称是否已存在
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_name", productDTO.getProductName());
        ProductInfo existingProduct = this.getOne(queryWrapper);
        if (existingProduct != null) {
            throw new RuntimeException("商品名称已存在");
        }

        // 构造新的商品对象并插入数据库
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName(productDTO.getProductName());
        productInfo.setCategoryId(productDTO.getCategoryId());
        productInfo.setBrand(productDTO.getBrand());
        productInfo.setDescription(productDTO.getDescription());
        productInfo.setPrice(productDTO.getPrice());
        productInfo.setStockQuantity(productDTO.getStockQuantity());
        productInfo.setImageUrls(productDTO.getImageUrls());
        productInfo.setCreateBy("admin"); // 这里假设管理员添加商品
        productInfo.setCreateTime(new Date());
        productInfo.setUpdateBy("admin");
        productInfo.setUpdateTime(new Date());
        productInfo.setOnSale(1); // 默认上架

        return this.save(productInfo);
    }

    @Override
    public Boolean editProduct(ProductDTO productDTO) {
        // 判断商品是否存在
        ProductInfo productInfo = this.getById(productDTO.getProductId());
        if (productInfo == null) {
            throw new RuntimeException("商品信息不存在");
        }

        // 更新商品信息
        productInfo.setProductName(productDTO.getProductName());
        productInfo.setCategoryId(productDTO.getCategoryId());
        productInfo.setBrand(productDTO.getBrand());
        productInfo.setDescription(productDTO.getDescription());
        productInfo.setPrice(productDTO.getPrice());
        productInfo.setStockQuantity(productDTO.getStockQuantity());
        productInfo.setImageUrls(productDTO.getImageUrls());
        productInfo.setUpdateBy("admin");
        productInfo.setUpdateTime(new Date());

        return this.updateById(productInfo);
    }

    @Override
    public Boolean deleteProduct(Long productId) {
        // 判断商品是否存在
        ProductInfo productInfo = this.getById(productId);
        if (productInfo == null) {
            throw new RuntimeException("商品信息不存在");
        }

        // 执行删除操作
        return this.removeById(productId);
    }

    @Override
    public Boolean putOnSale(Long productId) {
        // 判断商品是否存在
        ProductInfo productInfo = this.getById(productId);
        if (productInfo == null) {
            throw new RuntimeException("商品信息不存在");
        }

        // 设置商品为上架状态
        productInfo.setOnSale(1);
        productInfo.setUpdateBy("admin");
        productInfo.setUpdateTime(new Date());

        return this.updateById(productInfo);
    }

    @Override
    public Boolean putOffSale(Long productId) {
        // 判断商品是否存在
        ProductInfo productInfo = this.getById(productId);
        if (productInfo == null) {
            throw new RuntimeException("商品信息不存在");
        }

        // 设置商品为下架状态
        productInfo.setUpdateTime(new Date());

        return this.updateById(productInfo);
    }
}